//
//  messageVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/31.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "messageVC.h"
#import "messageModel.h"
#import "messageCell.h"
#import "messageDetiallViC.h"
@interface messageVC ()
@property (nonatomic, strong) NSMutableArray *orderList;
@property (nonatomic, assign) NSInteger page;
@end

@implementation messageVC

- (void)viewDidLoad {
    self.title = @"消息";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    CGRect frame = CGRectMake(0, 10, kScreenWidth, kScreenHeight - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 10);
    self.tableView = [[UITableView alloc]initWithFrame:frame];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.page = 1;
    [self addRefreshHeader];
    [self addRefreshFooter];
    [self confingureTableView];

    [self orderListRequest];
}

// MARK: - 下拉刷新
- (void)addRefreshHeader {
    [super addRefreshHeader];
}
- (void)addRefreshFooter {
    [super addRefreshFooter];
}

- (void)loadNew
{
    self.page = 1;
    [self orderListRequest];
}

- (void)loadMore
{
    if (!self.orderList.count) {
        [self endFooterRefresh];
        return;
    }
    self.page = self.page + 1;
    [self orderListRequest];
}
- (void)confingureTableView {
    // [super confingureTableView];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([messageCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([messageCell class])];
}
- (void)handleResponseData:(NSDictionary *)responseObject
{
    NSMutableArray *modelArray =  [messageModel mj_objectArrayWithKeyValuesArray:responseObject[@"articleItemList"]];
    if (self.page == 1) {
        [self.orderList removeAllObjects];
    }
    if (modelArray.count > 0) {
        [self.orderList addObjectsFromArray:(NSArray *)modelArray];
        if (modelArray.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }else {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}
#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.orderList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    messageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([messageCell class])];
    cell.model = [self.orderList objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    messageModel *model = [self.orderList objectAtIndex:indexPath.section];
    messageDetiallViC *VC = [messageDetiallViC new];
    VC.title = model.title;
    VC.content = model.content;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)orderListRequest {
    
    __weak typeof(self) weakSelf = self;
    [networkManger PostHttpDataWithUrlStr:[[apiManger shareApiManger] message] Dic:nil SuccessBlock:^(id responseObject) {
        [self endRefreshing];
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf handleResponseData:responseObject];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [self endRefreshing];
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
#pragma mark --- getter
- (NSMutableArray *)orderList {
    if (!_orderList) {
        _orderList = [NSMutableArray array];
    }
    return _orderList;
}

@end
